package com.cloudRestudy.payment.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloudRestudy.common.core.result.R;
import com.cloudRestudy.payment.constans.TestConstans;
import com.cloudRestudy.payment.entity.SysUser;
import com.cloudRestudy.payment.service.SysUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName : SysUserController
 * @Author : lby
 * @Date: 2021/7/8 11:08
 * @Description :
 */
@Slf4j
@RestController
@RequestMapping("user")
@AllArgsConstructor
public class SysUserController {
    private final SysUserService sysUserService;
    /**
     * 通过 DiscoveryClient 可以使customer  服务发现
     */
    private final DiscoveryClient discoveryClient;

    /**
     * 读取nacos 配置中心中的数据
     * @return
     */
    private final TestConstans testConstans;


    @GetMapping("/list")
    public R getUserList(){
        return R.ok( sysUserService.list());
    }

    @GetMapping("/page")
    public R getUserPage(Page page, SysUser sysUser){
        QueryWrapper<SysUser> query = Wrappers.query();
        return R.ok(sysUserService.page(page,query));
    }


    /**
     * eureka 服务发现
     * @return
     */
    @GetMapping("/client-info")
    public R getClientInfo(){
        // eureka中所有注册的服务列表
        List<String> services = discoveryClient.getServices();
        System.out.println(services);
        // 通过服务名称，获取信息
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT");
        instances.forEach(serviceInstance -> {
            System.out.println(serviceInstance.getUri());
            System.out.println(serviceInstance.getHost());
            System.out.println(serviceInstance.getPort());
            System.out.println(serviceInstance.getServiceId());
        });
        return R.ok();
    }

    /**
     * 读取nacos配置重心中的数据
     */
    @GetMapping("/get-nacos-config")
    public R getNacosConfig(){
        String age = testConstans.getAge();
        log.info("=======================age{}======================",age);
        String name = testConstans.getName();
        log.info("=======================name{}======================",name);
        String id = testConstans.getId();
        log.info("=======================id{}======================",id);
        return R.ok();
    }
}
